<?php

namespace app\admin\controller\system;

use app\common\controller\AdminController;
use app\common\service\TenImService;
use app\models\user\Users;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="prohibit")
 */
class Shielding extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\website\Prohibit();

    }

    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach ($list as &$item){
                $item['ip'] = getIp($item['ip']);
                $ip = explode(':',$item['ip']);
                if(isset($ip[1]) && $ip[1]){
                    $item['ip'] = $ip[1];
                }
            }
            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $post['operation'] = session('admin.username');
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }

            if ($save) {
                $this->forbidIp([$post['ip']]);
            }

            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    public function delete($id)
    {
        $row = $this->model->whereIn('id', $id)->select();
        $oldIp = [];
        foreach ($row as $item) {
            $oldIp = $item['ip'];
        }
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        if ($save) {
            $this->forbidIp($oldIp, 0);
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    public function forbidIp($ip, $ShutUpTime = 86400 * 365)
    {
        $params = [
            'GroupId' => TenImService::getGroupId(),
            'Ip' => $ip,
            'ShutUpTime' => $ShutUpTime
        ];
        if ($params['Ip']) {
            return;
        }
        $params['Members_Account'] = Users::whereIn('create_ip', $params['Ip'])->column('username');
        if (empty($params['Members_Account'])) return;
        unset($params['ip']);
        foreach ($params['Members_Account'] as &$item) {
            $account = TenImService::getAccount(1, $item);
            $item = $account['Nick'];
        }
        return TenImService::forbidSendMsg($params);
    }
}